<?php
namespace App\Service\Admin;

use App\Service\BaseService as Service;
use Illuminate\Support\Facades\DB;

class DocService extends Service
{
    //列表
    public function leftMenu()
    {
        $res = DB::table($this->dbDoc)->where('del', 0)->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有接口');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    //详情
    public function getOne($id)
    {
        $db = DB::table($this->dbDoc)->where('del', 0);
        if ($id) {
            $res = (array) $db->where('id', $id)->first();
        } else {
            $res = (array) $db->orderBy('id', 'asc')->first();
        }
        if (empty($res)) {
            return array('code' => -2, 'msg' => '该接口不存在');
        }
        //请求参数
        $pamArys = [];
        $pamOnes = DB::table($this->dbDocPam)->where('del', 0)
            ->where('doc_id', $res['id'])
            ->where('type', 1)
            ->get();
        if (count($pamOnes)) {
            foreach ($pamOnes as $v) {
                $v = (array) $v;
                $pamArys[] = [
                    'id' => $v['id'],
                    'field_name' => $v['field_name'],
                    'field_type' => $v['field_type'],
                    'must' => $v['must'],
                    'remark' => $v['remark'],
                ];
            }
        }
        //返回参数
        $backArys = [];
        $backOnes = DB::table($this->dbDocPam)->where('del', 0)
            ->where('doc_id', $res['id'])
            ->where('type', 2)
            ->get();
        if (count($backOnes)) {
            foreach ($backOnes as $v) {
                $v = (array) $v;
                $backArys[] = [
                    'id' => $v['id'],
                    'field_name' => $v['field_name'],
                    'field_type' => $v['field_type'],
                    'must' => $v['must'],
                    'remark' => $v['remark'],
                ];
            }
        }
        $arys = [
            'id' => $res['id'],
            'title' => $res['title'],
            'uri' => $res['uri'],
            'reqbody' => $res['reqbody'],
            'pamList' => $pamArys,
            'backbody' => $res['backbody'],
            'backList' => $backArys,
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    //新增
    public function add($pas)
    {
        $pas['addtime'] = toDate();
        $lastid = DB::table($this->dbDoc)->insertGetId($pas);
        if (!$lastid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    //修改
    public function modify($id, $pas)
    {
        $res = (array) DB::table($this->dbDoc)->where('id', $id)->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '接口不存在');
        }
        $pas['uptime'] = toDate();
        $up = DB::table($this->dbDoc)->where('id', $id)->update($pas);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}